package com.hushi.test;

import com.hushi.dao.CustomerDao;
import com.hushi.impl.CustomerDaoImpl;
import com.hushi.service.CustomerService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * IOC入门程序
 */

//@Scope("prototype"),多例
public class Demo1 {
    @Test
    public void test1(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//        用scope("prototype")实现多例
        for (int i = 0; i < 5; i++) {
            CustomerDao customerDao01 = (CustomerDao)ac.getBean("customerDao");
            System.out.println(customerDao01);
        }

    }

    @Test
    public void test2(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        CustomerDao customerDao = (CustomerDao)ac.getBean("customerDao");
        System.out.println(customerDao);
    }
    @Test
    public void test3(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        CustomerDao customerDao = (CustomerDao)ac.getBean("customerDao");
        System.out.println(customerDao);
        //手动销毁对象
//        ((AbstractApplicationContext)ac).registerShutdownHook();
    }

//    依赖注入测试
    @Test
    public void test4(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        CustomerService customerService = (CustomerService)ac.getBean("customerService");
        customerService.save();
    }

    public void test5(){}
}